Skip to content

Reject invalid patch document containers - #236

Open
JackyHsu-X wants to merge 1 commit into
stefankoegl:masterfrom
JackyHsu-X:fix/invalid-patch-document
Open

JackyHsu-X wants to merge 1 commit into
stefankoegl:masterfrom
JackyHsu-X:fix/invalid-patch-document

Conversation

@JackyHsu-X

Copy link
Copy Markdown

JsonPatch.from_string('{}') and JsonPatch.from_string('""') currently create a successful no-op patch. Other non-array JSON documents, such as null, booleans and numbers, leak TypeError during iteration. RFC 6902 section 3 defines a patch document as an array of operation objects, so these inputs should raise InvalidJsonPatch consistently.

Validate the document container before iterating over operations. Reject mappings and raw text in the Python constructor, and translate non-iterable inputs to InvalidJsonPatch. Existing list and tuple inputs retain their container identity and application behavior.

Regression tests cover direct construction, string and byte JSON input, unchanged documents when invalid input is applied in place, and valid empty/nonempty sequences. A Hypothesis property checks rejection of generated non-array JSON documents. The new regressions produce 9 failing subtests and 18 errors against the original implementation.

Validation on Windows / CPython 3.12.8:

  • python -m coverage run tests.py: 216 tests, OK (1 platform-specific skip), including doctests.
  • python -m coverage run --append property_tests.py: 21 property tests, OK (the data files were collected separately and combined locally).
  • Combined statement/branch coverage report: 99%; every added production line and both new error paths are covered.
  • Flake8 syntax/undefined-name checks and git diff --check pass.

Related: #132 handles strings encountered while iterating over operations; this also handles empty mappings/text and non-iterable document inputs. Iterator exhaustion is tracked separately in #234.

AI disclosure: OpenAI Codex assisted with investigation, implementation, tests and this description. The reported checks were executed locally. Other Python versions and platforms have not been tested locally.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The focused validation change has relevant regression coverage and no identified blocking issues.

No open issues 🎉

What changed in this PR

This PR makes invalid JSON Patch document containers consistently raise InvalidJsonPatch, while preserving existing list and tuple behavior.

Changes:

  • Reject mappings, raw text, and non-iterable constructor inputs before validating operations.
  • Add regression and property tests for invalid containers and valid sequences.
File Description
tests.py Covers invalid inputs, unchanged documents, and sequence preservation.
property_tests.py Checks rejection of generated non-array JSON documents.
jsonpatch.py Validates document containers before iterating over operations.

Review effort: Balanced

@coveralls

Copy link
Copy Markdown

Coverage Status

coverage: 100.0%. remained the same — JackyHsu-X:fix/invalid-patch-document into stefankoegl:master

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants